import { message } from 'antd';

const IP = "http://121.41.14.139:21021";
// const IP = "http://localhost:21021";
const tenantId = localStorage.getItem("tenantId");
const token = localStorage.getItem('access-token');
const request = (url, config) => {
    return fetch(IP + url, config).then(function (response) { return response.json() })
        .then((data) => {
            return data;
        })
        .catch((error) => {
            console.log(error);
            message.error('fetch data failed', error);
        });
};

// GET请求
export const get = (url) => {
    return request(url, {
        method: 'GET',
        headers: {
            'content-type': 'application/json',
            'Abp.TenantId': tenantId,
            'Abp-TenantId': tenantId,
            'Authorization': 'Bearer ' + token
        },
    });
};

// DELETE请求
export const del = (url) => {
    return request(url, {
        method: 'DELETE',
        headers: {
            'content-type': 'application/json',
            'Abp.TenantId': tenantId,
            'Abp-TenantId': tenantId,
            'Authorization': 'Bearer ' + token
        },
    });
};

// POST请求
export const post = (url, data = {}, method = 'POST') => {
    return request(url, {
        body: JSON.stringify(data),
        headers: {
            'content-type': 'application/json',
            'Abp.TenantId': data.tenant ? data.tenant : tenantId,
            'Abp-TenantId': data.tenant ? data.tenant : tenantId,
            'Authorization': 'Bearer ' + token
        },
        method
    });
};

// PUT请求
export const put = (url, data = {}) => {
    return request(url, {
        body: JSON.stringify(data),
        headers: {
            'content-type': 'application/json',
            'Abp.TenantId': data.tenant ? data.tenant : tenantId,
            'Abp-TenantId': data.tenant ? data.tenant : tenantId,
            'Authorization': 'Bearer ' + token
        },
        method: 'PUT'
    });
};

export const imgServer = IP + "/Home/GetImageAsync?sysRowId=";
export const uploadPic = async (files, objectId, objectName, objectField) => {
    let formData = new FormData();
    files.forEach(element => {
        formData.append("files", element.file);
    });
    formData.append("objectId", objectId);
    formData.append("objectName", objectName);
    formData.append("objectField", objectField);

    var resp = {};
    await fetch(IP + "/Home/UploadFilesAsync", {
        method: 'POST',
        headers: {
            'Authorization': 'Bearer ' + localStorage.getItem("accessToken"),
        },
        body: formData,
    }).then((response) => response.json())
        .then((responseData) => {
            resp = responseData;
        }).catch((err) => {
            Toast.fail("图片上传失败");
        });
    if (resp && resp.success) {
        return resp.result;
    }
    else {
        return "";
    }
};
export const mingzu = ("汉族、满族、蒙古族、回族、藏族、维吾尔族、苗族、彝族、壮族、布依族、侗族、瑶族、白族、土家族、哈尼族、哈萨克族、傣族、黎族、傈僳族、佤族、畲族、高山族、拉祜族、水族、东乡族、纳西族、景颇族、柯尔克孜族、土族、达斡尔族、仫佬族、羌族、布朗族、撒拉族、毛南族、仡佬族、锡伯族、阿昌族、普米族、朝鲜族、塔吉克族、怒族、乌孜别克族、俄罗斯族、鄂温克族、德昂族、保安族、裕固族、京族、塔塔尔族、独龙族、鄂伦春族、赫哲族、门巴族、珞巴族、基诺族").split('、');